package com.sxit.ecwx.core.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Method;
import java.sql.Clob;
import java.sql.SQLException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;


@SuppressWarnings("unchecked")
public class StringUtil extends StringUtils{
	public static String rootpath = "";
	
	/**
	 * 从InputStream中获取字符串对象
	 * @param is
	 * @param charset
	 * @return
	 */
	public static String valueOf(InputStream is,String charset){
		String result = null;
		if(is != null){
			try{
				byte[] buffer = new byte[1024];
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				int l = is.read(buffer);
				while(l!=-1){
					baos.write(buffer, 0, l);
					l = is.read(buffer);
				}
				is.close();
				result = new String(baos.toByteArray(),charset);
			}catch(Exception ex){
				ex.printStackTrace();
			}
		}
		return result;
	}
	
	/**
	 * 过滤查询条件中的非法字符
	 * @param str
	 * @return
	 */
	public static String getFilterString(String str) {
		str = str.replace('<', '_');
		str = str.replace('>', '_');
		str = str.replace('"', '_');
		str = str.replace('\'', '_');
		str = str.replace('%', '_');
		str = str.replace(';', '_');
		str = str.replace('(', '_');
		str = str.replace(')', '_');
		str = str.replace('&', '_');
		str = str.replace('+', '_');
		return str;
	}
	
	/**
	 * 首字母改为大�?
	 * @param str
	 * @return
	 */
	public static String firstToUpper(String str){
		str = str.trim();
		String ret = "";
		if(str.length()>=1){
			ret = str.substring(0,1).toUpperCase() + str.substring(1,str.length()); 
		}
		return ret;
	}
	
	/**
	 * 首字母改为小�?
	 * @param str
	 * @return
	 */
	public static String firstToLower(String str){
		str = str.trim();
		String ret = "";
		if(str.length()>=1){
			ret = str.substring(0,1).toLowerCase() + str.substring(1,str.length()); 
		}
		return ret;
	}





	/**
	 * 将时间转换为parrten2格式
	 * 
	 * @param t1
	 * @param parrten
	 * @param parrten2
	 * @return
	 */
	public static String getTime(String t1, String parrten, String parrten2) {
		return DateUtils.getTime(t1, parrten, parrten2);
	}

	/**
	 * �?测字符串s是否是a-z的字母组�?
	 * 
	 * @param s
	 * @return boolean
	 */
	public static boolean checkString(String s) {
		boolean tag = false;
		String regex = "\\p{Lower}+?";
		if (s != null && !"".equals(s)) {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(s);
			tag = m.matches();
		}
		return tag;
	}

	/**
	 * 根据模式regex，对字符串s，进行匹�?
	 * 
	 * @param s
	 *            字符�?
	 * @param regex
	 *            模式
	 * @return boolean true=符合，false=不符�?
	 */
	public static boolean checkString(String s, String regex) {
		boolean tag = false;
		if (s != null && !"".equals(s)) {
			Pattern p = Pattern.compile(regex);
			Matcher m = p.matcher(s);
			tag = m.matches();
		}
		return tag;
	}

	/**
	 * 将字符串转换成数�?
	 * 
	 * @param s
	 *            �?转换的字符串
	 * @return int，若该字符串不是数字，则返回�?1
	 */
	public static int strToInt(String s) {
		int temp;
		if (StringUtil.isNumeric(s.trim())) {
			temp = Integer.parseInt(s);
		} else {
			temp = 0;
		}
		return temp;
	}

	/**
	 * 将指定字符串重复count次并返回
	 * @param s
	 * @param count
	 * @return
	 */
	public static String repeat(String s,int count){
		StringBuffer result=new StringBuffer();
		for (int i = 0; i < count; i++) {
			result.append(s);
		}
		return result.toString();
	}
	/**
	 * 将字符串转换成小�?
	 * 
	 * @param s
	 * @return
	 */
	public static String toLowercase(String s) {
		String tag = null;
		if (s != null && !s.equals("")) {
			tag = s.toLowerCase();
		}
		return tag;
	}

	/**
	 * 生成SQL语句
	 * 
	 * @param tablename
	 *            表名
	 * @param sqlprotasis
	 *            条件
	 * @param columnname
	 *            字段�?
	 * @return 生成的SQL语句
	 */
	public static String generationSql(String tablename, String sqlprotasis,
			String[] columnname) {
		StringBuffer bufsql = new StringBuffer("select ");
		for (int i = 0; i < columnname.length; i++) {
			if (i == (columnname.length - 1)) {
				bufsql.append(columnname[i]);
			} else {
				bufsql.append(columnname[i] + ",");
			}
		}
		bufsql.append(" from " + tablename + sqlprotasis);
		return bufsql.toString();
	}

	/**
	 * 生成SQL语句
	 * 
	 * @param tablename
	 *            表名
	 * @param sqlprotasis
	 *            条件
	 * @param columnname
	 *            字段�?
	 * @return 生成的SQL语句
	 */
	public static String generationSql(int number, String tablename,
			String sqlprotasis, String[] columnname) {
		StringBuffer bufsql = new StringBuffer("select top " + number + " ");
		for (int i = 0; i < columnname.length; i++) {
			if (i == (columnname.length - 1)) {
				bufsql.append(columnname[i]);
			} else {
				bufsql.append(columnname[i] + ",");
			}
		}
		bufsql.append(" from " + tablename + sqlprotasis);
		return bufsql.toString();
	}

	/**
	 * 截字符串
	 * 
	 * @param s
	 *            字符�?
	 * @param n
	 *            截的位数
	 * @return
	 */
	public static String getNumString(String s, int n) {
		String tag;
		tag = s;
		if (s != null && !s.equals("")) {
			tag = tag.substring(0, n);
		} else {
			tag = "";
		}
		return tag;
	}

	/**
	 * 生成SQL语句
	 * 
	 * @param tablename
	 *            表名
	 * @param sqlprotasis
	 *            条件
	 * @return SQL语句
	 */
	public static String generationSql(String tablename, String sqlprotasis) {
		StringBuffer bufsql = new StringBuffer("select ");
		bufsql.append("* from " + tablename + sqlprotasis);
		return bufsql.toString();
	}

	/**
	 * 删除某目录下的指定文�?
	 * 
	 * @param filecatalog
	 *            目录列表
	 * @param filename
	 *            文件�? String[]
	 */
	public static void delFile(String filecatalog, String[] filename) {

		File delfile;
		String fname = "", name = "";
		try {
			for (int i = 0; i < filename.length; i++) {
				name = filename[i];
				if (name != null && !name.equals("")) {
					fname = rootpath + filecatalog + name;
					delfile = new File(fname);
					if (delfile.exists()) { // 判断文件是否存在
						delfile.delete();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 判断字符串是否是数字
	 * 
	 * @param s
	 * @return 是数字＝true;不是数字＝false
	 */
	public static boolean isNumeric(String s) {
		boolean flag = true;
		if (s != null && !s.equals("")) {
			char[] numbers = s.toCharArray();
			for (int i = 0; i < numbers.length; i++) {
				if (!Character.isDigit(numbers[i])) {
					flag = false;
					break;
				}
			}
		} else {
			flag = false;
		}
		return flag;
	}

	/**
	 * 字符串转化为长整行数�?
	 * 
	 * @param s
	 * @return
	 */
	public static long strToLong(String s) {
		long temp = 0;
		if (isNumeric(s)) {
			temp = Long.parseLong(s);
		}
		return temp;
	}
	
	

	/**
	 * 格式化输入文本，使输入和输出表现�?�?
	 * 
	 * @param input
	 *            输入
	 * @return buf
	 */
	public static String formatHTML(String input) {
		if (input == null || input.length() == 0) {
			return input;
		}
		// 建立�?个Stringbuffer 来处理输入的数据
		StringBuffer buf = new StringBuffer(input.length() + 6);
		char ch = ' ';
		// 处理非法字符�?
		for (int i = 0; i < input.length(); i++) {
			ch = input.charAt(i);
			if (ch == '<'){
				buf.append("&lt;");
				continue;
			}
			if (ch == '>'){
				buf.append("&gt;");
				continue;
			}
			
			if (ch == '\n'){
				buf.append("<br>");
				continue;
			}
			if (ch == '\''){
				buf.append("&acute;");
				continue;
			}
			if (ch == '"'){
				buf.append("&quot;");
				continue;
			}
			
			if (ch == ' '){
				buf.append("&nbsp;");
				continue;
			}
			buf.append(ch);
		}
		return buf.toString();
	}

	/**
	 * 分割字符�?
	 * 
	 * @param input
	 *            要分割的字符�?
	 * @return
	 */
	public static String formatBr(String input) {
		if (input == null || input.length() == 0) {
			return input;
		}
		// 建立�?个Stringbuffer 来处理输入的数据
		StringBuffer bf = new StringBuffer("");
		String from = "<>";
		StringTokenizer st = new StringTokenizer(input, from, true);
		while (st.hasMoreTokens()) {
			String tmp = st.nextToken();
			if (tmp != null && tmp.equals("<")) {
				String tmp2 = st.nextToken().toLowerCase();
				if (tmp2.equals("br")) {
					st.nextToken();
					bf = bf.append("");
				}
			} else {
				bf.append(tmp);
			}
		}
		return bf.toString();
	}

	/**
	 * encode转码
	 * 
	 * @param str
	 * @return 非空返回str，为空返�?""
	 */
	public static String encode(String str) {
		try {
			if (str != null) {
				return (new String(str.getBytes("iso-8859-1"), "gb2312"));
			} else
				return "";
		} catch (Exception e) {
			return e.toString();
		}
	}

	/**
	 * replace字符串替�? (例子：String a = replace("aaabbbccc","bbb","222"); ) 字符�?
	 * (返回值：a="aaa222ccc";)
	 * 
	 * @param str
	 *            字符�?
	 * @param substr
	 *            被替换的字符�?
	 * @param restr
	 *            要替换的字符�?
	 * @return
	 */
	public static String replace(String str, String substr, String restr) {
		String[] tmp = split(str, substr);
		String returnstr = null;
		if (tmp.length != 0) {
			returnstr = tmp[0];
			for (int i = 0; i < tmp.length - 1; i++)
				returnstr = dealNull(returnstr) + restr + tmp[i + 1];
		}
		return dealNull(returnstr);
	}

	/**
	 * @param source
	 *            �?要替换的StringBuffer
	 * @param strS
	 *            被替换的字符�?
	 * @param strD
	 *            用于替换的字符串
	 * @return 用strD字符串替换source里面strS字符串，例： replace(new
	 *         String("kkkk"),"k","e")，返�?"eeee"
	 */
	public static StringBuffer replaceAll(StringBuffer source, String strS,
			String strD) {
		if (strS == null || strD == null || strS.length() == 0
				|| strS.equals(strD))
			return source;
		int start = 0, offset = 0, tag = 0;
		while (source.indexOf(strS, tag) != -1) {
			start = source.indexOf(strS, tag);
			offset = start + strS.length();
			tag = start + strD.length();
			source.replace(start, offset, strD);
		}
		return source;
	}

	/**
	 * 根据�?始strStart和strEnd提取包含在source中的这段字符�?
	 * 
	 * @param source
	 *            源StringBuffer
	 * @param strStart
	 *            �?始标�?
	 * @param strEnd
	 *            结束标记
	 * @return 返回提取后的StringBuffer
	 */

	public static StringBuffer takeString(StringBuffer source, String strStart,
			String strEnd) {
		int start = source.indexOf(strStart) + strStart.length();
		int end = source.indexOf(strEnd);
		if ((end > start) && end != -1 && start != -1) {
			source = new StringBuffer(source.substring(start, end));
			return source;
		}
		return new StringBuffer();
	}

	/*
	 * Function name: dealNull Description: \u8655理空字符�? Input: String str
	 * Output: 不等於null的String
	 */
	public static String dealNull(Object str) {
		String returnstr = null;
		if (str == null) {
			returnstr = "";
		} else {
			returnstr = str.toString();
		}
		return returnstr;
	}
	
	/**
	 * Function name: split Description: \u5C07字符串劈\u958B成\u6578\u7D44 Input:
	 * \u5C07字符串source劈\u958B成\u6578\u7D44div(例子：String TTT[] =
	 * my_class.split("aaa:bbb:ccc:ddd",":") ; ) Output: \u6578\u7D44
	 * (返回值：TTT[0]="aaa"; TTT[1]="bbb"; TTT[2]="ccc"; TTT[3]="ddd"; )
	 */
	public static String[] split(String source, String div) {
		int arynum = 0, intIdx = 0, intIdex = 0, div_length = div.length();
		if (source.compareTo("") != 0) {
			if (source.indexOf(div) != -1) {
				intIdx = source.indexOf(div);
				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdx = source.indexOf(div, intIdx + div_length);
						arynum = intCount;
					} else {
						arynum += 2;
						break;
					}
				}
			} else
				arynum = 1;
		} else
			arynum = 0;

		intIdx = 0;
		intIdex = 0;
		String[] returnStr = new String[arynum];

		if (source.compareTo("") != 0) {

			if (source.indexOf(div) != -1) {

				intIdx = (int) source.indexOf(div);
				returnStr[0] = (String) source.substring(0, intIdx);

				for (int intCount = 1;; intCount++) {
					if (source.indexOf(div, intIdx + div_length) != -1) {
						intIdex = (int) source
								.indexOf(div, intIdx + div_length);

						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, intIdex);

						intIdx = (int) source.indexOf(div, intIdx + div_length);
					} else {
						returnStr[intCount] = (String) source.substring(intIdx
								+ div_length, source.length());
						break;
					}
				}
			} else {
				returnStr[0] = (String) source.substring(0, source.length());
				return returnStr;
			}
		} else {
			return returnStr;
		}
		return returnStr;
	}

	/**
	 * @param name
	 *            待切割的字符�?
	 * @param c
	 *            切割字符串的字符
	 * @return Strin[]
	 */
	public static String[] split(String name, char c) {
		// Figure out the number of parts of the name (this becomes the size
		// of the resulting array).
		if (name == null)
			return null;
		int size = 1;
		for (int i = 0; i < name.length(); i++) {
			if (name.charAt(i) == c) {
				size++;
			}
		}
		String[] propName = new String[size];
		// Use a StringTokenizer to tokenize the property name.
		StringTokenizer tokenizer = new StringTokenizer(name, String.valueOf(c));
		int i = 0;
		while (tokenizer.hasMoreTokens()) {
			propName[i] = tokenizer.nextToken();
			i++;
		}
		return propName;
	}

	/**
	 * 根据文件名获得文件的扩展�?
	 * 
	 * @param filename
	 * @return
	 */
	public static String getExt(String filename) {
		if (filename.indexOf(".") < 0) {
			return filename;
		}
		String[] strs = split(filename, '.');
		return strs[strs.length - 1];
	}

	/**
	 * 判断字符串是否包含该字符，如果有好写法，建议替换
	 * 
	 * @param strs
	 *            String[]
	 * @param str
	 *            String
	 * @return boolean
	 */
	public static boolean isInclude(String[] strs, String str) {
		if (strs.length == 0 || str == null || str.length() == 0) {
			return false;
		}
		str = str.toLowerCase();
		for (int i = 0; i < strs.length; i++) {
			if (str.equals(strs[i]))
				return true;
		}
		return false;
	}

//	public static String htmlToText(String inputHtml) {
//		String cleanText = "";
//		Parser parser = Parser.createParser(gbToIso(inputHtml), "GBK");
//		try {
//			TextExtractingVisitor visitor = new TextExtractingVisitor();
//			parser.visitAllNodesWith(visitor);
//			cleanText = ParserUtils.removeEscapeCharacters(visitor
//					.getExtractedText());
//		} catch (ParserException e) {
//			log.error(e.toString());
//		}
//		return isoToGb(cleanText);
//	}

	public static String gbToIso(String str) {
		try {
			if (str != null && !str.equals("")) {
				byte[] byteStr = str.getBytes("gb2312");
				return new String(byteStr, "ISO-8859-1");
			} else
				return "";
		} catch (Exception e) {
			return str;
		}
	}

	public static String isoToGb(String str) {
		try {
			if (str != null && !str.equals("")) {
				byte[] byteStr = str.getBytes("ISO-8859-1");
				return new String(byteStr, "gb2312");
			} else
				return "";
		} catch (Exception e) {
			return str;
		}
	}

	public static String isoToUTF8(String str) {
		try {
			if (str != null && !str.equals("")) {
				byte[] byteStr = str.getBytes("ISO-8859-1");
				return new String(byteStr, "UTF-8");
			} else
				return "";
		} catch (Exception e) {
			return str;
		}
	}
	// 不做任何处理，为了替换招聘的isoToGb
	public static String retString(String str) {
		return str;
	}

	public static String singleParameter(String name, String value) {
		if (name != null && !"".equals(name)) {
			return "&" + name + "=" + value;
		}
		return "";
	}

	// 生成单个字段条件
	public static String single1SearchContent(String searchname,
			String searchtype, String column) {
		if (searchname != null && !"".equals(searchname)) {
			if ("1".equals(searchtype)) {
				return " and " + column + " = '" + searchname + "'";
			} else {
				return " and " + column + " like '%" + searchname + "%'";
			}
		}
		return "";
	}

	// 生成单个字段条件
	public static String single2SearchContent(String searchname,
			String searchtype, String column) {
		if (searchname != null && !"".equals(searchname)) {
			if ("1".equals(searchtype)) {
				return " and " + column + " = '" + searchname + "'";
			} else if ("2".equals(searchtype)) {
				return " and " + column + " < '" + searchname + "'";
			} else {
				return " and " + column + " > '" + searchname + "'";
			}
		}
		return "";
	}

	public static String GBTounicode(String s) {
		StringBuffer ss = new StringBuffer();
		for (int i = 0; i < s.length(); ++i) {
			ss.append("\\u" + Integer.toHexString(s.charAt(i)));
		}
		String temp = new String(ss);
		return temp;

	}

	public static String unicodeToGB(String s) {
		StringBuffer sb = new StringBuffer();
		StringTokenizer st = new StringTokenizer(s, "\\u");
		while (st.hasMoreTokens()) {
			sb.append(st.nextToken());
		}
		return sb.toString();
	}
	



	/**
	 * 将指定字符串中的指定字符替换成同其对应的Ascii�?
	 * eg. '#'->'$35$'	其中35�?#的Ascii�?
	 * @param sString	源字符串
	 * @param sChar	特殊字符
	 * @return
	 */
	public static String encodeSpecialChar(String sString,char sChar){
		String sResult=sString;
		int iCode=sChar;
		sResult=StringUtils.replace(sResult,""+sChar,"$"+iCode+"$");
		return sResult;
	}
	public static String encodeSpecialChar(String sString,String sChar){
		return encodeSpecialChar(sString,sChar.charAt(0));
	}
	public static String decodeSpecialChar(String sString,String sChar){
		return decodeSpecialChar(sString,sChar.charAt(0));
	}
	/**
	 * 解析通过encodeSpecialChar方法编码过的特殊字符
	 * @param sString	源字符串
	 * @param sChar	特殊字符
	 * @return
	 */
	public static String decodeSpecialChar(String sString,char sChar){
		int iCode=sChar;
		String sResult=sString;
		sResult=StringUtils.replace(sResult,"$"+iCode+"$",""+sChar);
		return sResult;
	}
	/**
	//替代非法XML字符	
	**/
	public static String encodeXml(String sXml) {
		sXml=encodeSpecialChar(sXml,'&');
		sXml=encodeSpecialChar(sXml,'>');
		sXml=encodeSpecialChar(sXml,'<');
		return sXml ;
	}
	/**
	//转回原串	
	**/
	public static String decodeXml(String sXml) {
		sXml = decodeSpecialChar(sXml,'<') ;
		sXml = decodeSpecialChar(sXml,'>');
		sXml = decodeSpecialChar(sXml,'&');
		return sXml ;
	}

	/**
	 * 将制定的特殊符号编码
	 * @str �?要编码的字符�?
	 * @splitStr 逗号分隔特殊字符   "',%,&,*,/,\"
	 */
	public static String encodeChars(String str,String splitStr){
		String[] splitArray = splitStr.split(",");
		for(int i=0;i<splitArray.length;i++){
			String specialChar = splitArray[i];
			str = encodeSpecialChar(str,specialChar);
		}
		return str;
	}
	/**
	 * 将制定的特殊符号编码
	 * @str 逗号分隔特殊字符   "',%,&,*,/,\"
	 */
	public static String  decodeChars(String str,String splitStr){
		String[] splitArray = splitStr.split(",");
		for(int i=0;i<splitArray.length;i++){
			String specialChar = splitArray[i];
			str = decodeSpecialChar(str,specialChar);
		}
		return str;
	}
	
	
	public static  String encodeChar(String str){
		str = encodeSpecialChar(str,'\'');
		str = encodeSpecialChar(str,';');
		str = encodeSpecialChar(str,'&');
		str = encodeSpecialChar(str,'"');
		return str;
	}

	public static  String  decodeChar(String str){
		str = decodeSpecialChar(str,'\'');
		str = decodeSpecialChar(str,';');
		str = decodeSpecialChar(str,'&');
		str = decodeSpecialChar(str,'"');
		return str;
	}
	/**
	 * 将Map中的值连成字符串
	 * @param map
	 * @param separator
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String serializeMapValue(Map map,String separator){
		String sResult="";
		int i=0;
		for(Iterator iterator=map.values().iterator();iterator.hasNext();){			
			String sTmp=iterator.next().toString();
			sResult+=((i==0?"":separator)+sTmp);
			i++;
		}
		return sResult;		
	}

	/**
	 * 把Clob转换为String
	 * @param clob  要转换的Clob对象 
	 * @return 返回String类型 
	 */
	public static String ClobToString(Clob clob){
		if(clob==null)
			return "";
		String content = "";

		Reader instream;
		try {
			instream = clob.getCharacterStream();
			char buffer[] = new char[500000];
			instream.read(buffer);

			content = new String(buffer).trim();
					
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return content;
	}
	
	public static String stringTojeson(List<String> strList){
		 String x="";
		 int i=0;
		 for (String path : strList) {
			x+=(i++>0?",":"")+"'"+path+"'";
		}
		return x;
	}
	
	
	/**
	 * 不为�?
	 *@author   
	 *@date 	  2009-5-4
	 *@param str
	 *@return
	 *@return boolean
	 *@remark
	 */
	public static boolean isNotEmpty(String str){
		return str!=null && str.trim().length()>0;
	}
	/**
	 * 为空
	 *@author   
	 *@date 	  2009-5-4
	 *@param str
	 *@return
	 *@return boolean
	 *@remark
	 */
	public static boolean isEmpty(String str){
		return str==null ||  str.length()<=0 ;
	}
	
	/**
	 * 去掉字符串最后面字符
	 *@author   
	 *@date 	  2009-7-14
	 *@param sourceString
	 *@param cutString
	 *@return
	 *@return String
	 *@remark
	 */
	public static String delEndString(String sourceString,String cutString){
		String resultStr = null;
		if(isNotEmpty(sourceString) && isNotEmpty(cutString)){
			if(sourceString.endsWith(cutString)){
				resultStr = sourceString.substring(0,sourceString.length()- cutString.length());
			}else{
				resultStr = sourceString;
			}
		}
		return resultStr;
	}
	
	public static String delStartString(String sourceString,String cutString){
		String resultStr = null;
		if(isNotEmpty(sourceString) && isNotEmpty(cutString)){
			if(sourceString.startsWith(cutString)){
				resultStr = sourceString.substring(cutString.length(),sourceString.length());
			}else{
				resultStr = sourceString;
			}
		}
		return resultStr;
	}
	
	/**
	 * 将用逗号分隔的ids  1,2  转换成in括号内用的格�?  '1','2'
	 *@author   
	 *@date 	  2009-7-29
	 *@param ids
	 *@return
	 *@return String
	 *@remark
	 */
	public static String idsToWhereIn(String ids){
		
		StringBuffer sb = new StringBuffer();
		if(StringUtil.isNotEmpty(ids)){
			String [] arrId = ids.split(",");
			for(String strId : arrId){
				sb.append("'" + strId + "',");
			}
		}
		if(sb.toString().length() >0 && sb.toString().endsWith(",")){
			ids = StringUtil.delEndString(sb.toString(), ",");
		}
		return ids;
	}
	

	/**
	 * 比较两个IP地址
	 * @param ip1
	 * @param ip2
	 * @return  ip1>ip2   1
	 *          ip1<ip2   -1
	 *          ip1=ip2   0
	 */
	public static int compareIp(String ip1,String ip2){
		String tip1[] = StringUtil.split(ip1, ".");
		String tip2[] =  StringUtil.split(ip2, ".");
		int result = 0;
		for(int i=0;i<tip1.length;i++){
			int n1 = Integer.parseInt(tip1[i]);
			int n2 = Integer.parseInt(tip2[i]);
			if(n1<n2){
				result = -1;
				break;
			}
			if(n1>n2){
				result = 1;
				break;
			}
			if(n1==n2){
				continue;
			}
		}
		return result;
	}
	
	/**
	 * 判断IP地址ipX是否在ip1 和ip2的范围之�?
	 * @param ip1
	 * @param ip2
	 * @param ipX
	 * @return
	 */
	public static boolean isIpBetween(String ip1,String ip2,String ipX){
		int r1 = compareIp(ipX,ip1);
		if(r1 == 0)
			return true;
		
		int r2 = compareIp(ipX,ip2);
		if(r2 == 0)
			return true;
		
		if(r1 == 1 && r2 == -1)
			return true;
		
		if(r1 == -1 && r2 == 1)
			return true;
		
		return false;
		
	}
	
	
	

	/**
	 * 计算规则字符串的�?
	 * 使用eviMap中存在的占位符的值替换指定字符串表达式中的占位符并返回替换后的字符串
	 * 
	 *  String exp = "/userfiles/image/${date(yyyyMM)}/${date(dd)}/${filename}";
		Map ev = new HashMap();
		ev.put("date", new Date());
		ev.put("filename", 1234);
		String ret = StringUtil.evalExp(exp,ev);
		System.out.println(ret);
		输出结果�?/userfiles/image/201003/18/1234
	 * @param exp
	 * @param eviMap
	 * @return
	 */
	public static String evalExp(String exp,Map eviMap){
		// 匹配�?有的dv规则的正则表达式
		String patt = "\\$\\{[^}]+\\}";
		Pattern p3 = Pattern.compile(patt);
		Matcher m3 = p3.matcher(exp);
		while (m3.find()) {
			String rule = m3.group();
			Pattern pattern = Pattern.compile("[\\{ \\( \\) \\}]+");
			String[] strs = pattern.split(rule);
			String var = strs[1];//变量名称
			Object val = eviMap.get(var);
			String value = "";
			if(val!=null){
				if(val instanceof Date){
					String fm = (strs.length>=3?strs[2]:"yyyyMMdd");
					value = DateUtils.formatDate((Date) val, fm);
				}else{
					value = val.toString();
				} 
			}
			exp = StringUtil.replace(exp, rule, value);
		}
		return exp;
	}
//	public static void main(String[] args) {
//		Map map=new HashMap();
//		map.put("YEAR","2006");
//		map.put("MONTH","08");
//		map.put("DAY","23");
//		System.out.println(serializeMapValue(map,"-"));
//	}
	
	
	
}
